﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using GuRenFang.Service.Entity;
using GuRenFang.Service.IBiz;

namespace GuRenFang.Controllers.Management
{
    public class DefaultController : Controller
    {
        private readonly IContentInfoBiz _contentService;

        public DefaultController(IContentInfoBiz contentService)
        {
            _contentService = contentService;
        }

        public ActionResult Login()
        {
            return View();
        }


        [HttpPost]
        public ActionResult Login(FormCollection forms, string returnUrl)
        {
            string name = forms["username"];
            string password = forms["password"];
            string remember = forms["rememberMe"];
            Boolean isRemember = string.IsNullOrEmpty(remember) || remember == "on";
            //password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
            if (FormsAuthentication.Authenticate(name, password))
            {
                FormsAuthentication.SetAuthCookie(name, isRemember);
                return Redirect(!String.IsNullOrEmpty(returnUrl) ? returnUrl : FormsAuthentication.DefaultUrl);
            }
            else
                ViewBag.Error = "用户名或密码不正确！";

            return View();
        }

        [Authorize]
        public ActionResult Default()
        {
            return View();
        }

       
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Login", "Default");
        }

        [Authorize]
        public ActionResult AboutUs()
        {
            return View(_contentService.GetCompanyInfo());
        }

        [Authorize]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AboutUs(CompanyInfo companyInfo)
        {
            if (ModelState.IsValid)
            {
                _contentService.Update(companyInfo);
            }
            return RedirectToAction("Default");
        }


        [Authorize]
        public ActionResult Tips()
        {
            return View(_contentService.GetTipsInfo());
        }

        [Authorize]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Tips(TipsInfo tipsInfo)
        {
            if (ModelState.IsValid)
            {
                _contentService.Update(tipsInfo);
            }
            return RedirectToAction("Default");
        }
    }
}
